﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TheVerge.Phone.Controls;
using TheVerge.Base.Models;
using TheVerge.Base;
using TheVerge.Base.Platform;
using System.Windows.Data;

namespace TheVerge.Phone.Pages
{
    public partial class SectionsPage : MainPage
    {
        public SectionsPage()
        {
            InitializeComponent();
            DataContext = App.MainVM.Sections;
        }

        protected override MainPage.PageSection Section
        {
            get { return MainPage.PageSection.Sections; }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            App.MainVM.ActivePinnable = App.MainVM.Sections;
        }

        protected override void LoadFromQueryStrings(Dictionary<string, string> queryStrings)
        {
            App.MainVM.ActivePinnable.LoadFromPin(queryStrings);
        }

        private void thePiv_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (thePiv.SelectedItem != null)
            {
                var pivItem = thePiv.SelectedItem as PivotItem;
                var vm = pivItem.DataContext as IVMEntries;
                if (vm != null && vm.ItemsCount == 0)
                    vm.Refresh();
            }
        }

        private void Section_Click(object sender, RoutedEventArgs e)
        {
            var fe = sender as FrameworkElement;
            var entries = fe.DataContext as VMEntries<ArticleEntry>;

            App.MainVM.Articles = entries;

            NavigationService.Navigate(new Uri(@"/Pages/ArticlesPage.xaml", UriKind.Relative));
        }
    }

    public class BackgroundColorConverter : IValueConverter
    {
        public Brush HasItemsColor { get; set; }
        public Brush NoItemsColor { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (true.Equals(value))
                return HasItemsColor;
            else
                return NoItemsColor;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}